<template>
  <div>
    <div style="display: flex;justify-content: center;margin-top: 30px">
      <el-card style="width: 80%;margin: 10px">
        <div style="height: 50px;font-size: 25px">收藏</div>
        <div style="margin-bottom: 10px">
          <el-input style="width: 200px;margin: 0 5px" placeholder="查询店名" v-model="hotel_name"></el-input>
          <el-button type="primary" @click="load(1)">查询</el-button>
          <el-button type="success" @click="reset">重置</el-button>
        </div>
        <el-table ref="shopsTable" :data="tableData" stripe
                  :header-cell-style="{backgroundColor:'aliceblue',color:'#666'}">
          <el-table-column prop="hotel_id" label="酒店id" align="center"></el-table-column>
          <el-table-column prop="url" label="图片">
            <template v-slot="scope">
              <div style="display: flex;align-items: center">
                <el-image style="width: 50px;height: 50px;border-radius: 50%" v-if="scope.row.hotel_id"
                          :src="`http://localhost:8000/hotel_image_download/${scope.row.hotel_id}`"
                          :preview-src-list="[scope.row.hotel_id]"></el-image>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="hotel_name" label="酒店名称"></el-table-column>
          <el-table-column prop="level" label="酒店级别"></el-table-column>
          <el-table-column prop="location" label="酒店地址"></el-table-column>
          <el-table-column prop="intro" label="酒店简介" width="400px" align="center"></el-table-column>
          <el-table-column label="操作" width="200%" align="center">
            <template v-slot="scope">
              <el-button size="mini" type="primary" plain @click="canc(scope.row.hotel_id)">取消收藏</el-button>
              <el-button size="mini" type="primary" plain @click="goHotel(scope.row.hotel_id)">酒店详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div style="margin: 10px 0">
          <el-pagination
              @current-change="handleCurrentChange"
              :current-page="page_id"
              :page-sizes="[100, 200, 300, 400]"
              :page-size="page_size"
              layout="total,  prev, pager, next"
              :total="total">
          </el-pagination>
        </div>
      </el-card>
    </div>
    <el-dialog title="提示" :visible.sync="dialogSave" width="30%">
      <el-form ref="formRef" label-width="80px" style="display: flex; justify-content: center;">
        <div>
          <el-form-item prop="pay" style="text-align: center;">
            <div style="font-weight: bold;margin-right: 70px">确认取消收藏吗</div>
          </el-form-item>
          <div style="text-align: center; margin-bottom: 20px">
            <el-button type="primary" @click="cancel()">确认</el-button>
            <el-button @click="dialogSave = false">取 消</el-button>
          </div>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import user from "@/views/user.vue";

export default {
  name: 'userSave',
  data() {
    return {
      user: JSON.parse(localStorage.getItem('user') || '{}'),
      tableData: [], //表中数据
      page_id: 1,   //当前页码
      page_size: 5,    //每页数量
      total: 0,
      hotel_name: "",
      hotel_id: "",
      dialogSave: false,
    }
  },
  created() {
    this.load();
  },
  methods: {
    goHotel(hotel_id) {
      this.$router.push({
        path: '/hotelDetail',
        query: {
          id: hotel_id
        }
      })
    },
    load(page_id) {  //分页查询
      if (page_id) this.page_id = page_id
      this.$request.post('/user_collection_list/', {
        "username": this.user.username,
        "page_id": this.page_id,
        "page_size": this.page_size,
        "hotel_name": this.hotel_name,
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data.collection_list
          console.log("收藏数据", res.data.collection_list)
          this.total = res.data.item_amount
        } else if (res.code === '501') {

        } else if (res.code === '502') {
          this.$message.warning("没有搜索到相关酒店")
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleCurrentChange(page_id) {
      this.load(page_id)
    },
    reset() {
      this.hotel_name = ''
      this.load()
    },
    canc(hotel_id) {
      this.dialogSave = true
      this.hotel_id = hotel_id
    },
    cancel() {
      this.$request.post('/user_collection_cancel/', {
        "user_id": this.user.id,
        "hotel_id": this.hotel_id,
      }).then(res => {
        if (res.code === '200') {
          this.$message.success("取消收藏成功")
          // 刷新页面
          window.location.reload();
          this.dialogSave = false
        } else {
          this.$message(res.msg)
          this.dialogSave = false
        }
      }).catch(() => {
      })
    },
  }
}
</script>
<style scoped>

</style>
